package com.ctg.itrdc.sysmgr.portal.service.aop;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.ctg.itrdc.pasp.core.PageInfo;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserDTO;
import com.ctg.itrdc.sysmgr.portal.service.utils.PasswordHelper;

@Aspect
@Component
public class ReplacePasswordAOP {

	@Value("${password.algorithmName}")
	private String algorithmName;

	@Value("${password.hashIterations}")
	private int hashIterations;

//			+ "|| execution(* com.ctg.itrdc.sysmgr.portal.service.impl.*.*(..))"
//			+ "|| execution(* com.ctg.itrdc.sysmgr.portal.api.service.*.*(..)) "
	// TODO execution的内容待修改
	@Pointcut(value = " execution(* com.ctg.itrdc.pasp.core.IBaseService.*(..)) "
			+ "|| execution(* com.ctg.itrdc.sysmgr.portal.api.service.ISystemUserService.selectByOrgId(..))")
	private void pointcut() {
	}

	/**
	 * 查询 对password做脱敏处理
	 * 
	 * @param pjp
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "pointcut()")
	public Object replacePassword(ProceedingJoinPoint pjp) throws Throwable {
		Object result = pjp.proceed();
		if (result instanceof SystemUserDTO) {
			replacePassword(result);
		} else if (result instanceof List) {
			List<?> dtoList = (List<?>) result;
			replaceListPassword(dtoList);
		} else if (result instanceof PageInfo) {
			List<?> dtoList = ((PageInfo<?>) result).getList();
			replaceListPassword(dtoList);
		}
		return result;
	}

	/**
	 * 查询、插入时，对password进行加密
	 * 
	 * @param jp
	 */
	@Before(value = "pointcut()")
	public void EncryptPassword(JoinPoint jp) {
		Object[] args = jp.getArgs();
		if (ArrayUtils.isNotEmpty(args) && args[0] instanceof SystemUserDTO) {
			SystemUserDTO dto = (SystemUserDTO) args[0];
			encryptPassword(dto);
		} else if (ArrayUtils.isNotEmpty(args) && args[0] instanceof List) {
			List<?> dtoList = (List<?>) args[0];
			if (CollectionUtils.isNotEmpty(dtoList)) {
				if ((dtoList.get(0) instanceof SystemUserDTO)) {
					for (Object object : dtoList) {
						SystemUserDTO dto = (SystemUserDTO) object;
						encryptPassword(dto);
					}
				}
			}
		}
	}

	private void encryptPassword(SystemUserDTO dto) {
		String password = dto.getPassword();
		if (StringUtils.isNotBlank(password)) {
			dto.setPassword(PasswordHelper.encryptPassword(password, algorithmName, hashIterations));
		}
	}

	private SystemUserDTO replacePassword(Object result) {
		SystemUserDTO dto = (SystemUserDTO) result;
		dto.setPassword("******");
		return dto;
	}
	
	private void replaceListPassword(List<?> dtoList) {
		if (CollectionUtils.isNotEmpty(dtoList)) {
			if ((dtoList.get(0) instanceof SystemUserDTO)) {
				for (Object dto : dtoList) {
					replacePassword(dto);
				}
			}
		}
	}
}
